
import {Tooltip} from 'antd';
import moment from 'moment-timezone';

/**
 * 指标数据转换
 *
 * @param {data} array 数据list
 * @return {options} {返回转换后的数据}
 */
export default {};
export const groupByData = (datas, dateDims, prop, title) => {
    const data = datas.map(({...a}) => ({...a}));
    data.forEach(ele => {
        const eleKeys = Object.keys(ele);
        eleKeys.forEach(item => {
            if (ele[item]) {
                const Index = ele[item].indexOf('</a>');
                if (Index > -1) {
                    const titleIndex = ele[item].indexOf('>');
                    ele[item] = ele[item].slice(titleIndex + 1, Index);
                }
            }
            return ele[item];
        });
    });
    let keys = [];
    for (let i = 1; i < 17; i++) {
        keys.push(`dimension${i}`);
    }
    keys = keys.filter(ele => ele !== dateDims);
    const list = data || [];
    let groups = [];
    let dateList = [];
    list.forEach(v => {
        const key = {};
        const data = {};
        keys.forEach(k => {
            key[k] = v[k];
        });
        let group = groups.find(v => v.name === Object.values(key).filter(res => res)
            .join('-') + title);
        const name = Object.values(key).filter(i => i)
            .join('-') + title;
        if (!group) {
            group = {
                name,
            };
            groups.push(group);
        }
        dateList.push(v[dateDims]);
        const point = {
            xAixs: v[dateDims],
            value: v[prop]
        };
        group.point = group.point || [];
        group.point.push(point);
    });
    dateList = [...new Set(dateList)];
    groups = groups.map(ele => {
        let elePoint = ele.point;
        dateList.forEach(ele => {
            if (elePoint.findIndex(a => a.xAixs === ele) === -1) {
                elePoint.push({
                    xAixs: ele,
                    value: '0'
                });
            }
        });
        elePoint = elePoint.sort((a, b) =>
            parseInt(moment(b.xAixs, 'YYYYMMDDHHmmss').unix(), 10)
            - parseInt(moment(a.xAixs, 'YYYYMMDDHHmmss').unix(), 10));
        return ele;
    });
    return groups;
};
// export const groupByData = (datas, dateDims, prop, title) => {
//     let keys = [];
//     for (let i = 1; i < 17; i++) {
//         keys.push(`dimension${i}`);
//     }
//     keys = keys.filter(ele => ele !== dateDims);
//     const list = datas || [];
//     const groups = [];
//     list.forEach(v => {
//         const key = {};
//         const data = {};
//         keys.forEach(k => {
//             key[k] = v[k];
//         });
//         const beforName = Object.values(key).filter(res => res)
//             .join('-') + title;
//         let group = groups.find(v => v.name ===  beforName);
//         const name = beforName;
//         if (!group) {
//             group = {
//                 name,
//             };
//             groups.push(group);
//         }
//         const point = {
//             xAixs: v[dateDims],
//             value: v[prop]
//         };
//         group.point = group.point || [];
//         group.point.push(point);

//     });
//     return groups;
// };

export const generateColumns = (columns, period) => columns.map((item, index) => {
    item.dataIndex = item.key;
    // item.width = 120;
    item.title = (
        <Tooltip placement="topLeft" title={item.title}>
            <span className="tableColumns">
                {item.title}
            </span>
        </Tooltip>
    );
    item.render = (text, record) => {
        let str = text;
        if (text) {
            const Index = text.indexOf('</a>');
            if (Index > -1) {
                const titleIndex = text.indexOf('>');
                let title = text.slice(titleIndex + 1, Index);
                if (item.title.props.title === '日期' && period === '小时') {
                    title = `${title.slice(4, 6)}-${title.slice(6, 8)} ${title.slice(8, 10)}:00`;
                    str = str.slice(0, titleIndex + 1) + title + str.slice(Index);
                }
                return (
                    <Tooltip placement="topLeft" title={title}>
                        <div className="more-table" dangerouslySetInnerHTML={{__html: str}} />
                    </Tooltip>
                );
            }
            if (item.title.props.title === '日期' && period === '小时') {
                str = `${str.slice(4, 6)}-${str.slice(6, 8)} ${str.slice(8, 10)}:00`;
            }
        }
        return (
            <Tooltip placement="topLeft" title={str}>
                <div className="more-table">
                    {str}
                </div>
            </Tooltip>
        );
    };
    return item;
});

export const getFormatter = (newXvalue, options, searchLessonsNum) => params => {
    const date1 = params[0].axisValue;
    const date2 = newXvalue[options.xAxis.data.indexOf(date1)];
    let text = `<div>${date1}</div>`;
    let other = '';
    for (let index = 0; index < params.length; index++) {
        const element = params[index];
        if (element.seriesIndex < searchLessonsNum) {
            text = `${text}<div style="display:flex;align-items: center;">
                <div style="background: ${element.color};width:10px;height:10px;border-radius:10px;"></div>
                <span>${element.seriesName}</span> &nbsp;
                <span>${element.value}</span>
            </div>`;
        }
        else {
            other = `${other}<div style="display:flex;align-items: center;">
                <div style="background: ${element.color};width:10px;height:10px;border-radius:10px;"></div>
                <span>${element.seriesName}</span> &nbsp;
                <span>${element.value}</span>
            </div>`;
        }
    }
    text = `${text}<div>${date2}</div>`;
    text = text + other;
    return (text);
};

